package com.ad.pet.app.shopcar.confirm;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;

import butterknife.BindView;

/**
 * Created by SEELE on 2019/5/13.
 */

public class PayWebViewActivity extends AppToolbarActivity {
    @BindView(R.id.webView)
    WebView mWebView;
    private String mSns;
    private String mUrl;
    @BindView(R.id.progressBar1)
    ProgressBar pg;

    boolean isFirst = true;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mToolbar.setTitle(getString(R.string.pay));
        ImageView imageView = (ImageView) mToolbar.getLeftIcon();
        imageView.setImageResource(R.drawable.ic_x);

        mToolbar.setBackOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goOrderStatus(mActivity, "all",getString(R.string.all));
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected int contentLayout() {
        return R.layout.actvitity_pay_webview;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mStatusLayoutManager.showContent();
        mSns = getIntent().getStringExtra(Constant.ORDER_SN);
        mUrl = getIntent().getStringExtra(Constant.NAME);

        mWebView.loadUrl(mUrl);

        WebSettings webSettings = mWebView.getSettings();

//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
// 若加载的 html 里有JS 在执行动画等操作，会造成资源浪费（CPU、电量）
// 在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可

//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("/pay/paypal/cancel")) {

                    finish();
                } else {
                    view.loadUrl(url);
                }
                return super.shouldOverrideUrlLoading(view, url);
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                String myurl = url.substring(0, 22);
                if (url.contains("/pay/paypal/cancel")) {
                    Navigation.goOrderStatus(mActivity, "all",getString(R.string.all));
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                    return;
                } else if (myurl.contains("rabbos.com")) {

                    if (isFirst) {
                        isFirst = false;
                        Navigation.goCompleteOrder(mActivity, mSns);
                        setResult(RESULT_OK, new Intent());
                        finish();
                        return;
                    }
                }
                super.onPageFinished(view, url);
            }
        });


        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {


                if (newProgress == 100) {
                    if (pg != null)
                        pg.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    if (pg != null) {
                        pg.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                        pg.setProgress(newProgress);//设置进度值
                    }
                }

            }
        });


    }

    @Override
    public void initInject() {

    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        Navigation.goOrderStatus(mActivity, "all",getString(R.string.all));
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }
}
